package tmproject.hlhj.fhp.anwser.customview

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.widget.Toast
import com.lzy.okgo.model.Response
import com.lzy.okrx.adapter.ObservableResponse
import kotlinx.android.synthetic.main.dilaog_login.*
import tmproject.hlhj.fhp.anwser.R
import tmproject.hlhj.fhp.anwser.bean.BaseBean
import tmproject.hlhj.fhp.anwser.bean.LoginBean
import tmproject.hlhj.fhp.anwser.bean.SemsBean
import tmproject.hlhj.fhp.anwser.bean.SmsBean
import tmproject.hlhj.fhp.anwser.http.Apis
import tmproject.hlhj.fhp.anwser.http.BaseAction
import tmproject.hlhj.fhp.anwser.http.LoadingAction
import tmproject.hlhj.fhp.anwser.utils.PhoneUtils
import tmproject.hlhj.fhp.anwser.utils.SPHelper
import android.text.InputFilter
/**
 * Created by Never Fear   on 2018\8\20 0020.
Never More....
 */
class LoginDialog(private val c:Context) :Dialog(c, R.style.MyDialog){
    private var sec=60
    var listener:LoginListener?=null
    private val handler= @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (sec==0){
                sec=60
                btYzm.isClickable=true
                tvYzm.text="发送验证码"

            }else{
                this.sendEmptyMessageDelayed(1,1000)
                tvYzm.text=sec.toString()+"s"
                sec--
            }

        }
    }

    override fun dismiss() {
        super.dismiss()
        handler.removeMessages(1)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dilaog_login)
        etPhone.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(11)))
        etYzm.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6)))
        etPhone.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                if (etPhone.text.isEmpty()){
                    btCommit.setBackgroundResource(R.drawable.login_gray)
                    btYzm.isClickable=false
                    btCommit.isClickable=true
                    tvYzm.setTextColor(Color.parseColor("#999999"))
                }else{
                    btYzm.isClickable=true
                    btCommit.isClickable=true
                    btCommit.setBackgroundResource(R.drawable.login_commit_border)
                    tvYzm.setTextColor(Color.parseColor("#F70798"))
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })

        btDiss.setOnClickListener {
            dismiss()
        }
        btYzm.setOnClickListener {
            if (etPhone.text.toString().isEmpty()||etPhone.text.length!=11){
                Toast.makeText(c,"手机号码未填写或格式不正确",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            Apis.sendSms(etPhone.text.toString())
                    ?.let {
                        it.doOnSubscribe(LoadingAction(c))
                                .subscribe(object :BaseAction<Response<SmsBean>>{
                                    override fun httpSuccess(t: Response<SmsBean>) {
                                        handler.sendEmptyMessage(1)
                                        Toast.makeText(c,"发送成功",Toast.LENGTH_SHORT).show()
                                        btYzm.isClickable=false
                                    }
                                    override fun httpErroCode(t: BaseBean) {
                                        Toast.makeText(c,t.msg,Toast.LENGTH_SHORT).show()
                                    }
                                    override fun httpFail(e: Throwable?) {
                                        e?.printStackTrace()
                                        Log.e("okgo",e?.toString())
                                        Toast.makeText(c,"网络错误！！",Toast.LENGTH_SHORT).show()

                                    }
                                })
                    }
        }

        btCommit.setOnClickListener {
            if (etPhone.text.toString().isEmpty()){
                Toast.makeText(c,"手机号码为空",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (!PhoneUtils.isMobileNO(etPhone.text.toString())){
                Toast.makeText(c,"手机号格式不正确",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (etYzm.text.isEmpty()){
                Toast.makeText(c,"验证码不能为空",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (etYzm.text.length<4){
                Toast.makeText(c,"验证码不能少于4位",Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            Apis.login(etPhone.text.toString(),etYzm.text.toString())
                    ?.let {
                        it.doOnSubscribe(LoadingAction(c))
                                .subscribe(object :BaseAction<Response<LoginBean>>{
                                    override fun httpSuccess(t: Response<LoginBean>) {
                                        Toast.makeText(c,"登录成功",Toast.LENGTH_SHORT).show()
                                        SPHelper.saveToken(c,t.body().data.token)
                                        listener?.loginSuccess()
                                        dismiss()
                                    }

                                    override fun httpErroCode(t:BaseBean) {
                                        Toast.makeText(c,t.msg,Toast.LENGTH_SHORT).show()

                                    }

                                    override fun httpFail(e: Throwable?) {

                                    }
                                })
                    }
        }

    }
    interface LoginListener{
        fun loginSuccess()
    }
}